package com.banciyuan.bcywebview.biz.groupdetail;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.biz.setting.ChangeNickActivity;
import com.banciyuan.bcywebview.utils.http.CommonApiUtils;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.sp.SPHelper;
import com.banciyuan.bcywebview.utils.sp.SPConstant;
import com.banciyuan.bcywebview.utils.string.StringUtil;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by hy on 2015/5/22.
 */
public class TeamSettingActivity extends BaseActivity implements View.OnClickListener, ActionbarHelper.OnActionCallbacks {


    private RelativeLayout rlName;
    private RelativeLayout rlIntro;
    private TextView tvName;
    private TextView tvDesc;

    private String avater;
    private String name = "";
    private String desc = "";
    private String mGid;

    private ActionbarHelper mActionbarHelper;
    private View mActionBarView;

    public int CHANGNAME = 4;
    public static final int CHANGDESC = 5;

    private RequestQueue mQueue;


    private Response.Listener<String> onInfoSuccess;
    private Response.ErrorListener onError;
    private View hintView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teamsetting);
        initArgs();
        initProgressbar();
        initActionbar();
        initUi();
        initAction();
        initData();
    }

    @Override
    protected void initArgs() {
        mQueue = VolleyQueue.getRquest(this);
        Intent intent = getIntent();
        avater = intent.getStringExtra(gotoUtil.INTENT_VALUE_ONE);
        name = intent.getStringExtra(gotoUtil.INTENT_VALUE_TWO);
        desc = intent.getStringExtra(gotoUtil.INTENT_VALUE_THREE);
        mGid = intent.getStringExtra("gid");
    }

    @Override
    protected void initUi() {
        rlName = (RelativeLayout) findViewById(R.id.rl_name);
        rlIntro = (RelativeLayout) findViewById(R.id.rl_intro);
        tvName = (TextView) findViewById(R.id.tv_name);
        tvDesc = (TextView) findViewById(R.id.tv_desc);
        hintView = findViewById(R.id.rl_hint);
        if (SPHelper.getBoolean(this, SPConstant.TEAM_ACTIVITY_SHOW_HINT, true)) {
            hintView.setVisibility(View.VISIBLE);
        }
        findViewById(R.id.hint_img).setOnClickListener(this);
        if (!TextUtils.isEmpty(name)) {
            tvName.setText(name);
        }
        if (!TextUtils.isEmpty(desc)) {
            tvDesc.setText(Html.fromHtml(desc));
        }
        onInfoSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, getContext())) {
                    getInfoSuccess(response);
                }
            }
        };
        onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        };
    }

    private void getInfoSuccess(String result) {
        try {
            JSONObject mData = new JSONObject(result).getJSONObject(HttpUtils.UPLOAD_DATA);
            desc = StringUtil.getFromJson(mData, "group", "intro");
            tvDesc.setText(Html.fromHtml(desc));
        } catch (JSONException e) {
        }
    }

    @Override
    protected void initActionbar() {
        mActionBarView = findViewById(R.id.base_action_bar);
        mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.topic_set));
        mActionbarHelper.setActionCallbacks(this);
    }

    @Override
    protected void initAction() {
        rlName.setOnClickListener(this);
        rlIntro.setOnClickListener(this);
    }

    @Override
    protected void initProgressbar() {
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_name:
                doChangeName();
                break;
            case R.id.rl_intro:
                doChangeDesc();
                break;
            case R.id.hint_img:
                SPHelper.putBoolean(this, SPConstant.TEAM_ACTIVITY_SHOW_HINT, false);
                hintView.setVisibility(View.INVISIBLE);
                break;
        }
    }

    private void doChangeDesc() {
        Intent intent = new Intent(TeamSettingActivity.this, ChangeNickActivity.class);
        intent.putExtra("desc", desc);
        intent.putExtra("changeDesc", true);
        intent.putExtra("gid", mGid);
        startActivityForResult(intent, CHANGDESC);
    }

    private void doChangeName() {
        Intent intent = new Intent(TeamSettingActivity.this, ChangeNickActivity.class);
        intent.putExtra("name", name);
        intent.putExtra("fromTeam", true);
        intent.putExtra("gid", mGid);
        startActivityForResult(intent, CHANGNAME);
    }

    @Override
    public void onBackClick() {
        finish();
    }

    @Override
    public void onActionClick(int id) {

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (CHANGNAME == requestCode) {
            name = data.getStringExtra("name");
            tvName.setText(name);
        } else if (CHANGDESC == requestCode) {
            getInfo();
        }
    }

    private void getInfo() {
        CommonApiUtils.getTeamInfo(this, mQueue, onInfoSuccess, onError, mGid);
    }
}
